﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LibUpdate.Action
{
    /// <summary>
    /// 根据下载到本地的临时文件执行更新操作的控制器
    /// </summary>
   internal class ActionControl
    {

        //当前正在升级的版本
        public Version Update_Ver{get;set;}
        public UpdateConfig Config { get; set; }
        public bool CloseUpdate { get; set; }
    
        /// <summary>
        /// 更新通知
        /// </summary>
       internal event ActionEventHandler ActionInfo;
       /// <summary>
       /// 版本更新状态
       /// </summary>
       internal event VersionUpdateStatusChangedHandler VersionUpdateStatusChange;

       public ActionControl(UpdateConfig config)
       {
           Config = config;
       }
      
       
        
        /// <summary>
        /// 开始更新动作
        /// </summary>
        public void UpdateAction()
        {
            VersionUpdateStatusChangedEvent versionEvent = new VersionUpdateStatusChangedEvent();
            versionEvent.State = VersionDownState.EndUpdate;
            versionEvent.Version = Update_Ver;
            //更新文件
            for (int i = 0; i < Update_Ver.FileList.Count; i++)
            {
                // 执行更新
                if (FileActionState.Error == ActionFile(Update_Ver.FileList[i], i))
                {   //更新失败  
                   versionEvent.State = VersionDownState.Err;
                   break;
                }
            }
            VersionUpdateStatusChange(versionEvent);
       
        }

        /// <summary>
        /// 更新文件
        /// </summary>``
        private FileActionState ActionFile(File f, int index)
        {

            IUpdateAction ac = Get_Action(f.Type);
            ac.ActionFile = f;
            ac.AppPath = Config.ApplictionPath;

            ActionEventArgs e = new ActionEventArgs();

            e.FileIndex = index;

            //执行更新操作
            if (ActionTypeFile(ac))
            {
                e.ActionState = FileActionState.End;
            }
            else
            {
                e.ActionState = FileActionState.Error;
            }
            ActionInfo(e);
            return e.ActionState;
        }

     

        /// <summary>
        /// 根据文件需要的动作执行更新
        /// </summary>
        internal bool ActionTypeFile(IUpdateAction ac)
        {
            bool Action_Yes = true;
            switch (ac.ActionFile.ActionType)
            {
                case ActionType.Add:
                    if (!ac.Add())
                        Action_Yes = false;
                    break;
                case ActionType.Delete:
                    ac.Delete();
                    Action_Yes = false;
                    break;
                case ActionType.Edit:
                    if (!ac.Edit())
                        Action_Yes = false;
                    break;
                case ActionType.ExeC:
                    if (!ac.Exec())
                        Action_Yes = false;
                    break;
                case ActionType.Update://覆盖操作
                    if (!ac.Update())
                        Action_Yes = false;
                    break;
            }

            return Action_Yes;
        }

        /// <summary>
        /// 获得更新模式驱动
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private IUpdateAction Get_Action(FileType t)
        {
            IUpdateAction myaction;
            switch (t)
            {
                case FileType.BAT:
                    myaction = new BATAction();
                    break;
                case FileType.Directory:
                    myaction = new DirectoryAction();
                    break;
                case FileType.DLL:
                    myaction = new BinAction();
                    break;
                case FileType.EXE:
                    myaction = new BinAction();
                    break;
                case FileType.Image:
                    myaction = new BinAction();
                    break;
                case FileType.MDB:
                    myaction = new BinAction();
                    break;
                case FileType.SHP:
                    myaction = new BinAction();
                    break;
                case FileType.SQL:
                    myaction = new BinAction();
                    break;
                case FileType.TXT:
                    myaction = new BinAction();
                    break;
                case FileType.UpdateInfoFile:
                    myaction = new BinAction();
                    break;
                case FileType.VBS:
                    myaction = new BinAction();
                    break;
                case FileType.XML:
                    myaction = new XMLAction();
                    break;
                default:
                    myaction = new BinAction();
                    break;
            }
            myaction.VerStrDir = Update_Ver.Directory;
            return myaction;
        }

   }
}
